package com.waijiaojun.tpo.service;

import com.waijiaojun.tpo.model.ArticleModel;
import com.waijiaojun.tpo.utils.AliyunOssUtil;
import com.waijiaojun.tpo.vo.BootstrapTableDataResponse;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.waijiaojun.tpo.entity.Scenario;
import com.waijiaojun.tpo.repository.ScenarioDao;

@Service
public class ScenarioService {

    @Resource
    private ScenarioDao scenarioDao;

    public int insert(Scenario pojo) {
        return scenarioDao.insert(pojo);
    }

    public int insertSelective(Scenario pojo) {
        return scenarioDao.insertSelective(pojo);
    }

    public int insertList(List<Scenario> pojos) {
        return scenarioDao.insertList(pojos);
    }

    public int update(Scenario pojo) {
        return scenarioDao.update(pojo);
    }

    public BootstrapTableDataResponse<Scenario> findAll(Integer offset, Integer pageSize, String categoryId, String order, String sort, String search) {
        RowBounds rowBounds = new RowBounds(offset, pageSize);
        BootstrapTableDataResponse<Scenario> response = new BootstrapTableDataResponse<Scenario>();

        Long total = scenarioDao.findAllCount(categoryId, search);
        response.setTotal(total);

        List<Scenario> list = scenarioDao.findAll(categoryId, order, sort, search, rowBounds);
        if (list == null) {
            list = new ArrayList<>();
        }
        response.setRows(list);
        return response;
    }

    public Scenario findById(Long id) {
        return scenarioDao.findById(id);
    }

    public Scenario save(Scenario scenario) {

        if (scenario.getId() == null) {
            scenarioDao.insertSelective(scenario);// 新增
        } else {
            scenarioDao.update(scenario);// 更新
        }

        // 保存文件到OSS，并更新url
        String audioUrl = null;
        String coverUrl = null;
        String videoUrl = null;
        String review_image_url = null;
        if (scenario.getAudioFile() != null && !scenario.getAudioFile().isEmpty()) {
            audioUrl = AliyunOssUtil.uploadScenarioFile(scenario.getAudioFile(), scenario.getId());
        }
        if (scenario.getCoverFile() != null && !scenario.getCoverFile().isEmpty()) {
            coverUrl = AliyunOssUtil.uploadScenarioFile(scenario.getCoverFile(), scenario.getId());
        }
        if (scenario.getVideoFile() != null && !scenario.getVideoFile().isEmpty()) {
            videoUrl = AliyunOssUtil.uploadScenarioFile(scenario.getVideoFile(), scenario.getId());
        }
        if (scenario.getReviewImageFile() != null && !scenario.getReviewImageFile().isEmpty()) {
            review_image_url = AliyunOssUtil.uploadScenarioFile(scenario.getReviewImageFile(), scenario.getId());
        }
        if (audioUrl != null || coverUrl != null || videoUrl != null || review_image_url != null) {
            if (audioUrl != null) {
                scenario.setAudioUrl(audioUrl);
            }
            if (coverUrl != null) {
                scenario.setCoverUrl(coverUrl);
            }
            if (videoUrl != null) {
                scenario.setVideo_url(videoUrl);
            }
            if (review_image_url != null) {
                scenario.setReview_image_url(review_image_url);
            }
            scenarioDao.update(scenario);
        }
        return scenario;
    }

    public void delete(Long id) {
        scenarioDao.delete(id);
    }
}
